package com.xingxing.Activity;

import android.content.Intent;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.gson.Gson;
import com.xingxing.Activity.databinding.ActivityLoginBinding;
import com.xingxing.Bean.LoginBean;
import com.xingxing.Util.CommonUtils;
import com.xingxing.Util.APIConfig;
import com.xingxing.Interface.OkSuccessInterface;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static com.xingxing.Util.CommonUtils.*;

public class LoginActivity extends AppCompatActivity {
    private ActivityLoginBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        CommonUtils.init(getApplication());

        try {
            binding.etUserAccount.setText(Objects.requireNonNull(GetUserInfo()).getNickName());

        } catch (Exception e) {

        } finally {

        }
        // 登 录
        binding.btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (binding.etUserAccount.getText().toString().isEmpty()
                        || binding.etUserPassword.getText().toString().isEmpty()) {
                    Toast.makeText(LoginActivity.this, "账号或密码为空！", Toast.LENGTH_SHORT).show();
                } else {
                    binding.btnLogin.setText("正在登录。。。");
                    binding.btnLogin.setEnabled(false);
                    Map<String, Object> map = new HashMap<>();
                    map.clear();
                    map.put("userAccount", binding.etUserAccount.getText().toString());
                    map.put("userPassword", binding.etUserPassword.getText().toString());
                    OKJsonPost(LoginActivity.this, new Gson().toJson(map), APIConfig.Login, new OkSuccessInterface() {
                        @Override
                        public void OnSuccess(String json) {
                            LoginBean loginBean = new Gson().fromJson(json, LoginBean.class);
                            Toast.makeText(LoginActivity.this, loginBean.getStatus(), Toast.LENGTH_SHORT).show();
                            if (loginBean.getStatus().equals("登录成功")) {
                                SaveKey("UserInfo", "info", json);
                                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                                startActivity(intent);
                                finish();
                            } else {

                            }
                        }
                    });
                    binding.btnLogin.setEnabled(true);
                    binding.btnLogin.setText("登  录");
                }
            }
        });

        // 注 册
        binding.btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }
}